import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/check_in_detail/due_in_add/tab_pane.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";

class DueInAdd extends StatefulWidget {
  final int initialIndex;
  final int checkInId;
  final int roomId;
  const DueInAdd({super.key, required this.initialIndex, required this.checkInId, required this.roomId});

  @override
  State<StatefulWidget> createState() => _DueInAddState();

}

class _DueInAddState extends State<DueInAdd> {
  final List<String> _tabs = ["单笔添加", "批量添加"];
  List _list = [];
  double _totalMoney = 0;

  void _onChange(List billList) {
    _list.addAll(billList);
    setState(() {
      _totalMoney = 0;
      _list.sort((a, b) {
        int aTimestamp = DateTime.parse(a["noticeDate"]).millisecondsSinceEpoch;
        int bTimestamp = DateTime.parse(b["noticeDate"]).millisecondsSinceEpoch;
        print("序列：${DateTime.parse(a["noticeDate"]).millisecondsSinceEpoch}, >>>> $aTimestamp");
        return aTimestamp.compareTo(bTimestamp);
      });
      print("排序：>>>$_list");
      for (var item in _list) {
        _totalMoney += double.parse(item["money"]);
      }
    });
  }

  void _onRemove(int index) {
    setState(() {
      _totalMoney -= double.parse(_list[index]["money"]);
      _list.removeAt(index);
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print("init:>>>>>>>>>>${widget.checkInId}, ${widget.roomId}");
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      initialIndex: widget.initialIndex ?? 0,
      length: _tabs.length,
      child: Scaffold(
        appBar: AppBar(
          title: const Text("房间详情"),
          centerTitle: true,
          backgroundColor: ThemeVars.colorPrimaryDark,
          foregroundColor: Colors.white,
          leading: IconButton(
            icon: const Icon(Icons.arrow_back_ios_new),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
        ),
        body: Stack(
          // shrinkWrap: true,
          children: [
            Container(
              height: MediaQuery.of(context).size.height - 48,
              width: MediaQuery.of(context).size.width,
              padding: EdgeInsets.only(top: 48),
              child: TabBarView(
                children: [
                  TabPane(type: DueInAddType.single, list: _list, onChange: _onChange, totalMoney: _totalMoney, onRemove: _onRemove, checkInId: widget.checkInId, roomId: widget.roomId),
                  TabPane(type: DueInAddType.multiple, list: _list, onChange: _onChange, totalMoney: _totalMoney, onRemove: _onRemove, checkInId: widget.checkInId, roomId: widget.roomId),
                ],
              ),
            ),
            Positioned(
              top: 0,
              left: 0,
              width: MediaQuery.of(context).size.width,
              child: Container(
                height: 48,
                decoration: BoxDecoration(
                  color: Colors.white,
                ),
                child: TabBar(
                  tabs: _tabs.map((String name) => Tab(text: name)).toList(),
                  onTap: (index) {},
                ),
              ),
            ),
          ]
        ),
      ),
    );
  }

}